﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SFWS.XHandler.Core;
using JKF.Substructure;
using JKF.BusinessLayer.BLL;
using JKF.Entities.Model;
using JKF.Entities;
using System.Data;
namespace JKF.HandlerLayer.Customer
{
   public partial class Dic_GroupHandler:AHttpHandler
    {
       private static bllDIC_GROUP bllDic_Group = new bllDIC_GROUP();
        public override void ProcessRequest()
        {
            //所有错误和异常将抛出异常.正常执行程序则在方法则返回数据
            try
            {
                this.Manager(Utils.GetActionName);
            }
            catch (Exception ex)
            {
                //将错误抛到客户端(以Json格式)
                Utils.ToClient(true, ex.Message);
                //写日志
                AppLogger.Log(ex.Message, ex);
            }
        }

        /// <summary>
        /// 添加会员分组
        /// </summary>
        public void InsertGroup()
        {
            try
            {
                var strScr = P("moduleObj");
                var dic_GroupInfo = this.BuildDIC_GROUPinfoByJson(JsonSerializer.Deserialize<DIC_GROUPinfo>(strScr), false);
                var tid = dic_GroupInfo.GROUP_KEY;
                bllDic_Group.Add(dic_GroupInfo);
                var pk = dic_GroupInfo.GROUP_KEY;
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Message = "添加成功.", Data = pk }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        /// <summary>
        /// 修改会员分组名称
        /// </summary>
        public void EditGroup()
        {
            try
            {
                var strScr = P("moduleObj");
                var dic_GroupInfo = this.BuildDIC_GROUPinfoByJson(JsonSerializer.Deserialize<DIC_GROUPinfo>(strScr), true);
                var tid = dic_GroupInfo.GROUP_KEY; 
                bllDic_Group.Update(dic_GroupInfo);
                var pk = dic_GroupInfo.GROUP_KEY;
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Message = "修改成功.", Data = pk }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        /// <summary>
        /// 删除会员级别名称
        /// </summary>
        public void DeleteDic_Group()
        {
            try
            {
                var group_Code = P("id");
                //bllDic_Group.Delete(new Guid(level_Code));
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode =bllDic_Group.TransactionSQLDeleteBLL(new Guid(group_Code)) , Data = group_Code }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        /// <summary>
        /// 获取店面列表数据
        /// </summary>
        public void GetDic_Groups()
        {
            var obj = new PagerList();

            int pageCount = 0;
            int rowCount = 0;

            obj.Entity = from a in bllDic_Group.GetDic_GroupsBLL(P("NAME"), P("PageIndex").ToInt32(), P("PageSize").ToInt32(), ref pageCount, ref rowCount).AsEnumerable()
                         select new
                         {
                             GROUP_KEY = a.Field<Guid>("GROUP_KEY"),
                             NAME = a.Field<string>("NAME"),
                             MEMO = a.Field<string>("MEMO")
                         };

            obj.RowCount = rowCount;
            obj.PageCount = pageCount;
            ResponseJsonObject = obj;
        }

        /// <summary>
        /// 从Json对象构建SYS_MODULE
        /// </summary>
        /// <param name="shopInfo">实体对象</param>
        /// <param name="isEdit">是否为编辑</param>
        /// <returns></returns>
        public DIC_GROUPinfo BuildDIC_GROUPinfoByJson(DIC_GROUPinfo dic_GroupInfo, bool isEdit)
        {//SHOP_CODE, SHOP_NAME, LINKMAN, PHONE, AREA_CODE, ADDRESS, POSTAL_CODE, MEMO, SMS_AVAIBLE, SMS_SENDED, SINGNATURE, USER_CREATE, DATE_CREATE, DATE_UPDATE
            var entity = new DIC_GROUPinfo();
            if (!isEdit) // 添加
            {
                entity.GROUP_KEY = new Guid(DB.GetNewGuid(DB.HR)); //Guid.NewGuid();
            }
            else // 修改
            {
                entity.GROUP_KEY = dic_GroupInfo.GROUP_KEY;
            }
            entity.NAME = dic_GroupInfo.NAME;
            entity.MEMO = dic_GroupInfo.MEMO;
            entity.SHOP_CODE = bllSYS_USER.CurrentUser.SHOP_CODE;
            entity.USER_CREATE = bllSYS_USER.CurrentUser.USER_KEY; // 获取当前用户guid
            entity.DATE_CREATE = System.DateTime.Now;
            return entity;
        } 

        /// <summary>
        /// 根据模块group_code获得实体模块信息
        /// </summary>
        public void GetGroupByCode()
        {
            try
            {
                var scid = P("moduleKey");
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Data = bllDic_Group.GetModel(new Guid(scid)) }));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }

        /// <summary>
        /// 检查已输入名称是否存在
        /// </summary>
        public void CheckGroupName()
        {
            try
            {
                var group_name = P("GROUP_NAME");
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Ok", Data = bllDic_Group.ExistsGroupNameBLL(group_name)}));
            }
            catch (Exception exp)
            {
                Response.Write(JsonSerializer.Serialize(new ResponseInf { MessageCode = "Error", Message = exp.Message.FilterJs() }));
            }
        }
    }
}
